﻿using System.Linq;
using System.Threading.Tasks;
using CRM.Extensions;
using CRM.Services.SocialMarketing;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace CRM.Controllers.SocialMarketing
{
    [ApiController]
    [Route("api/[controller]")]
    public class SocialMarketingRecycleBinController :BaseController
    {
        private readonly MyDbContext _dbContext;
        private readonly SocialMarketingTasksServer _server;

        public SocialMarketingRecycleBinController(MyDbContext dbContext, SocialMarketingTasksServer server)
        {
            _dbContext = dbContext;
            _server = server;
        }

        [HttpGet]
        public async Task<BaseController.ResultStruct> RecycleBinIndex()
        {
            return Success(await _dbContext.SocialMarketingTasks.IgnoreQueryFilters().Where(m => m.IsDeleted).ToPaginateAsync(Request));
        }

        [HttpPost("{id:int}")]
        public async Task<BaseController.ResultStruct> RecycleBinRestore(int id)
        {
            var row = await _dbContext.SocialMarketingTasks.IgnoreQueryFilters().FirstAsync(m => m.Id == id);
            if (row == null) return Error(message: "无权操作");
            
            await _server.Restore(row);
            return Success();
        }

        [HttpDelete("{id:int}")]
        public async Task<BaseController.ResultStruct> RecycleBinDelete(int id)
        {
            var row = await _dbContext.SocialMarketingTasks.IgnoreQueryFilters().FirstAsync(m => m.Id == id);
            if (row == null) return Error(message: "无权操作");

            await _server.ForceRemove(row);
            return Success();
        }
    }
}